package com.ljkj.cordial.chat.activity;


import android.app.Activity;
import android.content.Intent;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.View;

import com.ljkj.cordial.chat.R;
import com.ljkj.cordial.chat.adapter.FileMAdapter;
import com.ljkj.cordial.chat.adapter.FileMPagerAdapter;
import com.ljkj.cordial.chat.base.BaseActivity;
import com.ljkj.cordial.chat.bean.Canstants;
import com.ljkj.cordial.chat.bean.PayLoad;
import com.yanzhenjie.album.AlbumFile;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 文件管理器
 */
public class FileManageActivity extends BaseActivity implements FileMAdapter.OnFileItemClickListener {


    @BindView(R.id.tablayout)
    TabLayout tablayout;
    @BindView(R.id.view_pager)
    ViewPager viewPager;
    private FileMPagerAdapter fileMPagerAdapter;
    private List<String> selected = new ArrayList<>();

    @Override
    public int getContentLayoutId() {
        return R.layout.activity_file_manage;
    }

    @Override
    public void initView() {
        ButterKnife.bind(this);
        getToolbar().setTitle("文件选取");
        fileMPagerAdapter = new FileMPagerAdapter(getSupportFragmentManager(), this);
        viewPager.setAdapter(fileMPagerAdapter);
        viewPager.setOffscreenPageLimit(4);
        viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tablayout));
        tablayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                viewPager.setCurrentItem(tab.getPosition());
                fileMPagerAdapter.getItemClickListener(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        getToolbarRightTv().setText("确认");
        getToolbarRightTv().setVisibility(View.VISIBLE);
        getToolbarRightTv().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!selected.isEmpty()) {
                    Intent intent = getIntent();
                    intent.putExtra(Canstants.FILE_PATH, TextUtils.join(",", selected));
                    setResult(Canstants.REQUEST_CODE_302, intent);
                }

                finish();
            }
        });
    }

    public static void chooseFile(Activity activity) {
        Intent intent = new Intent(activity, FileManageActivity.class);
        activity.startActivityForResult(intent, Canstants.REQUEST_CODE_302);
    }

    public static String[] onResultPath(int requestCode, int resultCode, Intent data) {
        if (requestCode == Canstants.REQUEST_CODE_302 && resultCode == Canstants.REQUEST_CODE_302 && data != null) {
            return data.getStringExtra(Canstants.FILE_PATH).split(",");
        }
        return null;
    }

    @Override
    public void onItem(FileMAdapter adapter, View v, AlbumFile item, int position, boolean isChecked) {
        if (selected.size() >= 3 && isChecked) {
            showToastSHORT("最多选取3个");
            adapter.notifyItemChanged(position, PayLoad.create(PayLoad.UNSELECT));
            return;
        }
        if (isChecked) {
            if (selected.contains(item.getPath())) {
                selected.remove(item.getPath());
            }
            selected.add(item.getPath());
        } else {
            if (selected.contains(item.getPath())) {
                selected.remove(item.getPath());
            }
        }
    }
}
